package com.sc.everyonerentboot.util;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.sc.everyonerentboot.pojo.AbstractParameter;
import com.sc.everyonerentboot.pojo.ComputerParameters;
import com.sc.everyonerentboot.pojo.HeadsetParameters;
import com.sc.everyonerentboot.pojo.PhoneTabletParameters;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;

//自定义反序列化器
public class ParameterUtil extends StdDeserializer<AbstractParameter> {
    public ParameterUtil(){
        super(AbstractParameter.class);
    }

    @Override
    public AbstractParameter deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        JsonNode node=jsonParser.getCodec().readTree(jsonParser);
        System.out.println(node);
        String type=node.get("Type").asText();
        if ("computer".equals(type)){
            System.out.println("computer");
            ComputerParameters parameter=new ComputerParameters();
            parameter.setType(node.get("Type").asText());
            parameter.setComputerType(node.get("computerType").asText());
            parameter.setModel(node.get("model").asText());
            parameter.setCpu(node.get("cpu").asText());
            parameter.setRam(node.get("ram").asText());
            parameter.setGraphicsCard(node.get("graphicsCard").asText());
            parameter.setDiskCapacity(node.get("diskCapacity").asText());
            parameter.setScreenRefreshRate(node.get("screenRefreshRate").asInt());
            try {
                parameter.setReleaseDate(sdf.parse(node.get("releaseDate").asText()));
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            parameter.setScreenColorGamut(node.get("screenColorGamut").asText());
            parameter.setProcessor(node.get("processor").asText());
            parameter.setScreenResolution(node.get("screenResolution").asText());
            return parameter;
        }else if("phone".equals(type)){
            System.out.println("phone");
           PhoneTabletParameters parameter=new PhoneTabletParameters();
            parameter.setType(node.get("Type").asText());
            parameter.setModel(node.get("model").asText());
            parameter.setNetworkMode(node.get("networkMode").asText());
            parameter.setStorageCapacity(node.get("storageCapacity").asText());
            parameter.setRam(node.get("ram").asText());
            parameter.setUnlockMethod(node.get("unlockMethod").asText());
            try {
                parameter.setReleaseDate(sdf.parse(node.get("releaseDate").asText()));
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            parameter.setRefreshRate(node.get("refreshRate").asInt());
            parameter.setScreenSize(BigDecimal.valueOf(node.get("screenSize").asDouble()));
            parameter.setSource(node.get("source").asText());
            parameter.setProcessor(node.get("processor").asText());
            parameter.setDataCableInterface(node.get("dataCableInterface").asText());
            parameter.setWlanFunction(node.get("wlanFunction").asInt());
            return parameter;
        }else if("headset".equals(type)){
            HeadsetParameters parameter=new HeadsetParameters();
            parameter.setType(node.get("Type").asText());
            parameter.setHeadsetType(node.get("headsetType").asText());
            parameter.setModel(node.get("model").asText());
            parameter.setWaterproof(node.get("waterproof").asInt());
            try {
                parameter.setReleaseDate(sdf.parse(node.get("releaseDate").asText()));
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            return parameter;
        }
        throw new IOException("Unknown type for AbstractParameter deserialization");
    }
}
